package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public List<AddressBook> getAddressList(AddressBook addressBook) {
        List<AddressBook> list = addressBookMapper.getAddressList(addressBook);
        return list;
    }

    @Override
    public void addAddress(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.addAddress(addressBook);
    }

    @Override
    public AddressBook getAddress(Integer id) {
        AddressBook addressBook = addressBookMapper.getAddress(id);
        return addressBook;
    }

    @Override
    public void updateAddress(AddressBook addressBook) {
        addressBookMapper.updateAddress(addressBook);
    }

    @Override
    public void setDefaultAddress(AddressBook addressBook) {
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.updateAddressByUserId(addressBook);

        addressBook.setIsDefault(1);
        addressBookMapper.updateAddress(addressBook);
    }

    @Override
    public void deleteAddress(Long id) {
        addressBookMapper.deleteAddress(id);
    }

    @Override
    public AddressBook getDefaultAddress() {
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        AddressBook addressBook1=addressBookMapper.getDefaultAddress(addressBook);
        return addressBook1;
    }
}
